<?php
ob_start();

session_start();
//error_reporting(E_ALL|E_STRICT);
//error_reporting(0);
error_reporting(1);
defined('SITE_BASE_DIR') || define('SITE_BASE_DIR', realpath(dirname(__FILE__)));
include SITE_BASE_DIR."/config/config.php";
include SITE_BASE_DIR."/config/init.php";

if($_SESSION['lang']=='en'){
    include SITE_BASE_DIR."/lang/lang.php";
    ob_start("obCallBackEn");
}else{
    include SITE_BASE_DIR."/lang/lang.php";
    ob_start("obCallBackVn");
}

$params =explode('/',input('params','GET'));

switch($params[0]) {
    case "sendFiend": {
            include SITE_MODULE_DIR.'/contact/functions/contact-functions.php';
            if(isset($_POST["post_contact"])) {
                $subject = strip_tags($_POST["title"]);
                $name = strip_tags($_POST["name"]);
                $email_to = strip_tags($_POST["email_to"]);
                $title = $subject;
                $urlDetail = $_POST["url"];
                $content="<b>Mail liên hệ từ:</b> ".$name."<br><b>email:</b> ".$email."<br><b>[Tiêu đề]: ".$title."</b><br><b>[Địa chỉ]: <a href=\"".$urlDetail."\">$urlDetail<a/></b><br><b>[Nội dung]:</b> ".strip_tags($_POST["content"]);
                if(smtpGmailer($CONFIG["sender_email"],$email_to,$name, $subject,$content)) {
                    echo 'susscess';die;
                }else {
                    echo 'die';die;
                }
                unset($_POST);
            }
            break;
        }case "newsLetter": {
                global $db;
                $email = $_REQUEST['email'];
                $rsCheck = $db->query_first("SELECT id from newsletter WHERE 1 AND email='$email'");
                if(intval($rsCheck['id'])<=0){
                    $db->query("INSERT INTO `newsletter` (`email`) VALUES ('$email')");
                }
                break;
            }case "saveFavourite": {
                    global $db;
                    if(check_member_login()){
                        $pid = $_REQUEST['id'];
                        $uid = $_SESSION['login']['id'];

                        $rsCheck = $db->query_first("SELECT id from favourite_property WHERE 1 AND pid=$pid AND uid=$uid");

                        if(intval($pid)>0){
                            if(intval($rsCheck['id'])<=0){
                                $db->query("INSERT INTO `favourite_property` (`pid`,`uid`) VALUES ($pid,$uid)");
                            }
                        }else{
                            echo 'error';
                        }
                    }else{
                        echo 'login';
                    }
                    break;
        }case "setTabProject": {
                if(intval($_REQUEST['tab'])==1){
                    $_SESSION['projectTab'] = 1;
                }else if(intval($_REQUEST['tab'])==2){
                    $_SESSION['projectTab'] = 2;
                }else if(intval($_REQUEST['tab'])==3){
                    $_SESSION['projectTab'] = 3;
                }else{
                    $_SESSION['projectTab'] = 1;
                }
                break;
        }case "getCategoryType": {
                global $db,$CONFIG;
                if(isset($_REQUEST['type'])) {
                    $cat_parent = intval($_REQUEST['type']);
                }else {
                    $cat_parent = 1;
                }
                $rsCatProperty = $db->query("SELECT cat_id,cat_title FROM cat_property WHERE cat_parent=$cat_parent ORDER BY cat_order");
                $option = '<option value="0">[Chọn loại địa ốc]</option>';
                while($rs=$db->fetch_array($rsCatProperty)) {
                    if(isset($_REQUEST['cat_id']) && $_REQUEST['cat_id']==$rs['cat_id']) {
                        $option .= '<option selected="selected" value="'.$rs['cat_id'].'">'.$rs['cat_title'].'</option>';
                    }else {
                        $option .= '<option value="'.$rs['cat_id'].'">'.$rs['cat_title'].'</option>';
                    }
                }
                echo $option;

                break;
        }case
    "member": {
                include SITE_MODULE_DIR.'/member/functions/member-functions.php';
                include SITE_MODULE_DIR.'/member/functions/post-property-functions.php';
                switch($params[1]) {
                    case "getCategoryType": {
                            getCategoryType();
                            break;
                        }
                        case "getGeo": {
                                getAjaxGeographic();
                                break;
                            }
                        }
                        break;

        }case
    "getDistrict": {

                if(isset($_REQUEST['city'])) {
                    $geo_parent = intval($_REQUEST['city']);
                    $option = '<option value="0">[Tất cả]</option>';
                    if(intval($geo_parent)>-1) {
                        $rsGeoProperty = $db->query("SELECT geo_id,geo_title FROM geographic WHERE geo_parent=$geo_parent ORDER BY geo_order");
                        while($rs=$db->fetch_array($rsGeoProperty)) {
                            if(isset($_REQUEST['geo_id'])) {
                                if($arrGeoID[0]==$rs['geo_id'] || $arrGeoID[1]==$rs['geo_id']) {
                                    $option .= '<option selected="selected" value="'.$rs['geo_id'].'">'.$rs['geo_title'].'</option>';
                                }else {
                                    $option .= '<option value="'.$rs['geo_id'].'">'.$rs['geo_title'].'</option>';
                                }
                            }else {
                                $option .= '<option value="'.$rs['geo_id'].'">'.$rs['geo_title'].'</option>';
                            }
                        }
                    }
                    echo $option;
                }

                break;
            }case
    "ajaxcaptcha": {

                    if(isset($params[1])&&strtolower($params[1])==strtolower($_SESSION['captcha_val'])) {
                        echo "true";
                    }else {
                        echo "false";
                    }
                    break;

                        }case
    "checkemailexist": {

                                include SITE_MODULE_DIR.'/register/functions/register-functions.php';

                                if(!valid_email($params[1])) {
                                    echo "no";
                                }else {
                                    if(check_email_exist($params[1])) {
                                        echo "true";
                                    }else {
                                        echo "false";
                                    }
                                }
                                break;
                                    }case
    "checkusernameexist": {

                                            include SITE_MODULE_DIR.'/register/functions/register-functions.php';

                                            if(eregi('[^a-zA-Z0-9_-]',$params[1])||strlen($params[1])<6||strlen($params[1])>20) {
                                                echo "no";
                                            }else {
                                                if(check_username_exist($params[1])) {
                                                    echo "true";
                                                }else {
                                                    echo "false";
                                                }
                                            }
                                            break;
                                                }case
    "getdistrict": {

                                                        include SITE_MODULE_DIR.'/register/functions/register-functions.php';
                                                        $rsgeo = get_all_geo(intval($params[1]));
                                                        while($geo=$db->fetch_array($rsgeo)) {
                                                            if($geo["geo_id"]==intval($params[2])) {
                                                                echo "<option value='".$geo["geo_id"]."' selected='selected'>".$geo["geo_title"]."</option>";
                                                            }else {
                                                                echo "<option value='".$geo["geo_id"]."' >".$geo["geo_title"]."</option>";
                                                            }

                                                        }
                                                        break;
                                                            }case
    "get_property_sell": {

                                                                    include SITE_MODULE_DIR.'/property/functions/property-functions.php';

                                                                    $maxPrice = $params[3]+10;
                                                                    $minPrice = $params[3]-10;

                                                                    $current_id = $params[4];

                                                                    if($params[1]==1) {
                                                                        $sql = "AND (price < $maxPrice AND price > $minPrice ) AND type='sell' AND id!= $current_id";
                                                                    }else if($params[1]==2) {
                                                                        $sql = 'AND create_date LIKE "%'.get_my_date($property["create_date"],'Y-m-d').'%" AND type='."'sell' AND id!= $current_id";
                                                                    }else {
                                                                        $sql = "AND (price < $maxPrice AND price > $minPrice ) AND type='sell' AND id!= $current_id";
                                                                    }

                                                                    $rs = get_all_property($sql,$params[2]);

                                                                    echo "<div class='tab_sanBDS_info'>";

                                                                    while($property=$db->fetch_array($rs["property"])) {

                                                                        $_SESSION["tt"]++;
                                                                        ?>

<div class="div_content1">
    <div class="images2"><a href="<?php echo get_link("property","detail",$property['title_link']); ?>" class="iamges_ct" title="<?php echo htmlentities_utf8($property['title']);?>"><img src="<?php echo SITE_URL.get_property_thumb($property["thumb"],'87-87') ?>" width="72px" height="71px" /></a></div>
    <div class="infocontent2">
        <a href="<?php echo get_link("property","detail",$property['title_link']); ?>" title="<?php echo htmlentities_utf8($property['title']);?>"><b>Tab <?php echo $params[1].cutString($property["title"],100); ?></b></a>
        <p style="color:#777;"><img src="<?php echo SITE_IMAGES_URL; ?>/point.gif" />Giá: <?php echo get_price_string($property["price"]);?></p>
        <p style="color:#777;">	<img src="<?php echo SITE_IMAGES_URL; ?>/point.gif" />Loại BĐS: <a class="textgray" href="#"><?php echo get_cat_property($property["cat_id"]); ?></a></p>
    </div>
    <div class="showcontent" id="neocat-<?php echo $_SESSION["tt"]; ?>">
        <img src="<?php echo SITE_IMAGES_URL; ?>/luu.gif" />&nbsp;<a href="#">Lưu</a><a href="#">&nbsp;l&nbsp;</a><img src="<?php echo SITE_IMAGES_URL; ?>/chitiet.gif" />&nbsp;<a href="<?php echo get_link("property","detail",$property['title_link']); ?>" title="<?php echo htmlentities_utf8($property['title']);?>">Chi tiết</a>
    </div>
</div>

                                                                        <?php  		 }

                                                                    echo "</div>";


                                                                    if($rs["total"]>1) {

                                                                        echo paging_ajax($rs["page"],$rs["total"],$params[1],"sell");

                                                                    }

                                                                    break;
                                                                        }case
    "get_property_rent": {

                                                                                include SITE_MODULE_DIR.'/property/functions/property-functions.php';

                                                                                $maxPrice = $params[3]+10;
                                                                                $minPrice = $params[3]-10;

                                                                                $current_id = $params[4];

                                                                                if($params[1]==1) {
                                                                                    $sql = "AND (price < $maxPrice AND price > $minPrice ) AND type='rent' AND id!= $current_id";
                                                                                }else if($params[1]==2) {
                                                                                    $sql = 'AND create_date LIKE "%'.get_my_date($property["create_date"],'Y-m-d').'%" AND type='."'rent' AND id!= $current_id";
                                                                                }else {
                                                                                    $sql = "AND (price < $maxPrice AND price > $minPrice ) AND type='rent' AND id!= $current_id";
                                                                                }

                                                                                $rs = get_all_property($sql,$params[2]);

                                                                                echo "<div class='tab_sanBDS_info'>";

                                                                                while($property=$db->fetch_array($rs["property"])) {

                                                                                    $_SESSION["tt"]++;
                                                                                    ?>

<div class="div_content2">

    <div class="infocontent3">
        <a href="<?php echo get_link("property","detail",$property['title_link']); ?>" title="<?php echo htmlentities_utf8($property['title']);?>"><b>Tab <?php echo $tab.cutString($property["title"],100); ?></b></a>
        <p style="color:#777;"><img src="<?php echo SITE_IMAGES_URL; ?>/point.gif" />Giá: <?php echo get_price_string($property["price"]);?></p>
        <p style="color:#777;"><img src="<?php echo SITE_IMAGES_URL; ?>/point.gif" />Loại BĐS: <a class="textgray" href="#"><?php echo get_cat_property($property["cat_id"]); ?></a></p>
    </div>

    <div class="showcontent" id="neocat-<?php echo $_SESSION["tt"]; ?>">
        <img src="<?php echo SITE_IMAGES_URL; ?>/luu.gif" />&nbsp;<a href="#">Lưu</a><a href="#">&nbsp;l&nbsp;</a><img src="<?php echo SITE_IMAGES_URL; ?>/chitiet.gif" />&nbsp;<a href="<?php echo get_link("property","detail",$property['title_link']); ?>" title="<?php echo htmlentities_utf8($property['title']);?>">Chi tiết</a>
    </div>
</div>

                                                                                    <?php  		 }

                                                                                echo "</div>";


                                                                                if($rs["total"]>1) {

                                                                                    echo paging_ajax($rs["page"],$rs["total"],$params[1],"rent");

                                                                                }

                                                                                break;
                                                                                    }

                                                                                }

                                                                                ?>